package com.wing.product.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.request.ProductDetailForm;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.service.ProductDetailService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-产品明细表管理", tags = "#/setting/productDetail")
@RestController("productDetailController")
@RequestMapping("productCab/admin/productDetail")
@RequiredArgsConstructor
public class ProductDetailController {

    private final ProductDetailService productDetailService;



    @ApiOperation(value = "查询所有产品明细表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String skuCode
            ,Long productTypeId
            ,Long productCategoryId
            ,Long distributionDemandId){

        Page<ProductDetail> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort",StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        paramMap.put("skuCode",skuCode);
        paramMap.put("productTypeId",productTypeId);
        paramMap.put("productCategoryId",productCategoryId);
        paramMap.put("distributionDemandId",distributionDemandId);
        //搜索

        IPage<ProductDetailVO> list = productDetailService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加产品明细表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProductDetailForm.Add.class) ProductDetailForm productDetailForm) {
        if (productDetailService.add(productDetailForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改产品明细表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(ProductDetailForm.Update.class) ProductDetailForm productDetailForm) {
        ProductDetail productDetail = productDetailService.getById(productDetailForm.getId());
        if(productDetail == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productDetailForm, productDetail, copyOptions);
        if (productDetailService.updateById(productDetail)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除产品明细表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productDetailId", value = "产品明细表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long productDetailId) {
        if (productDetailService.removeById(productDetailId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
    @ApiOperation(value = "查询所有产品", notes = "查询")
    @GetMapping("/getAll")
    public JsonResult<ProductDetailVO> getAll(Integer inoutType){
        List<ProductDetailVO> productDetail = productDetailService.getProductDetailByInoutType(inoutType);
        return JsonResult.success(productDetail);
    }

    /**
     * 提供给签收刀具入库类型查询产品使用
     * @param skuCode
     * @return
     */
    @ApiOperation(value = "查询产品相关信息", notes = "查询")
    @GetMapping("/getProductInfoBySkuCode")
    public JsonResult<ProductDetailVO> getProductInfoBySkuCode(@RequestParam("skuCode") String skuCode){
        ProductDetailVO info = productDetailService.getProductInfoBySkuCode(skuCode);
        return JsonResult.success(info);
    }

    @GetMapping("/updatePicBySkuCode")
    public JsonResult updatePicBySkuCode(String skuCode,String picUrl){
        ProductDetail productDetail=new ProductDetail();
        productDetail.setProductPic(picUrl);
        productDetailService.update(productDetail,new QueryWrapper<ProductDetail>().eq("sku_code",skuCode));
        return JsonResult.success("修改成功");
    }

}
